package com.opal.system.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.ReturnMessageData;
import com.opal.system.entity.table.OpalUser;
import com.opal.system.entity.table.PKOrg;
import com.opal.system.entity.table.QCBasedoc;
import com.opal.system.entity.table.QCTlevm;
import com.opal.system.mapper.mysql.OrganizationMapper;
import com.opal.system.mapper.mysql.QCBasedocMapper;
import com.opal.system.mapper.mysql.QCTlevmMapper;
import com.opal.system.mapper.oracle.QCTlevmOracleMapper;
import com.opal.system.service.QCService;
import com.opal.utils.PageVOUtils;
import com.opal.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class QCServiceImpl implements QCService {

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private QCBasedocMapper qcBasedocMapper;

    @Autowired
    private QCTlevmMapper qcTlevmMapper;
    @Autowired
    private QCTlevmOracleMapper qCTlevmOracleMapper;

    public OpalUser getUser() {
        Subject subject = SecurityUtils.getSubject();
        return (OpalUser) subject.getPrincipal();
    }

    @Override
    public String selectByName(String name) {
        return organizationMapper.selectByName(name);
    }

    @Override
    public List<PKOrg> selectBypkOrg() {
        return organizationMapper.selectByAll();
    }

    @Override
    public List<QCBasedoc> selectByDocname(String docname) {
        return qcBasedocMapper.selectByPrimaryKey(docname);
    }

    @Override
    public QCBasedoc selectByBasedoc() {
        return qcBasedocMapper.selectByAll();
    }

    @Override
    public PageVO<QCTlevm> selectByTlevm(int page, int rows, QCTlevm qcTlevm) {
        int firstIndex = ((page - 1) * rows);
        int count = getCount(qcTlevm);
        List<QCTlevm> qcTlevmList = qcTlevmMapper.selectByObject(firstIndex, rows, qcTlevm);
        return PageVOUtils.success(count, qcTlevmList);
    }

    @Override
    public int getCount(QCTlevm qcTlevm) {
        return qcTlevmMapper.getCount(qcTlevm);
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ReturnMessageData deleteByTlevm(List<String> nos) {
        OpalUser user = getUser();
        if (getUser() != null) {
            int result = qcTlevmMapper.deleteByPrimaryKey(nos);
            if (result > 0) {
                log.info("用户: "+user.getUserName() + " 执行了 " + result + " 条品保管理记录删除");
                return new ReturnMessageData(WSC.RESTPONSE_OK, "品保数据_成功删除 "+result+ "条记录!");
            }
            return new ReturnMessageData(WSC.REQUEST_ERROR, "品保数据_删除失败!");
        }
        return new ReturnMessageData(WSC.REQUEST_NOT_FOUNT, "登录已过期,正在为您转回登录界面");
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ReturnMessageData insertSelectiveTlevm(QCTlevm qcTlevm) {
        if (getUser() != null) {
            qcTlevm.setNos(qcTlevm.getUniqueNos());
            int result = qcTlevmMapper.insertSelective(qcTlevm);
            if (result > 0) {
                return new ReturnMessageData(WSC.RESTPONSE_OK,"品保数据_添加成功!");
            }return new ReturnMessageData(WSC.REQUEST_ERROR, "品保数据_添加失败!");
        }return new ReturnMessageData(WSC.REQUEST_NOT_FOUNT, "登录已过期,正在为您转回登录界面");

    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ReturnMessageData updateByTlevmSelective(QCTlevm qcTlevm) {
        if (getUser() != null) {
            OpalUser user = getUser();
            String regex = "^[a-z0-9A-Z]+$";
            if (!qcTlevm.getPkOrg().matches(regex)) {
                qcTlevm.setPkOrg(selectByName(qcTlevm.getPkOrg()));
            }
            int result = qcTlevmMapper.updateByPrimaryKeySelective(qcTlevm);
            if (result > 0) {
                log.info("用户: "+user.getUserName() + " 执行了单号为: " + qcTlevm.getNos() + " 的品保管理记录更新");
                return new ReturnMessageData(WSC.RESTPONSE_OK, "品保数据_更新成功!");
            } else
                return new ReturnMessageData(WSC.REQUEST_ERROR, "品保数据_更新失败!");
        }
        return new ReturnMessageData(WSC.REQUEST_NOT_FOUNT, "登录已过期,正在为您转回登录界面");
    }

    @Override
    public ReturnMessageData getQCTlevmByRemark1Service(String remark1) {
        List<QCTlevm> qcTlevm= qCTlevmOracleMapper.getQCTlevmByRemark1(remark1);
        if(qcTlevm==null || qcTlevm.size()<=0){
            return  new ReturnMessageData(WSC.REQUEST_BAD_REQUEST,"未查询到相关的品号",null);
        }
        return new ReturnMessageData(WSC.RESTPONSE_OK,"查询成功",qcTlevm);
    }

    @Override
    public List<QCTlevm> selectALlTlevmService(QCTlevm qcTlevm) {
        List<QCTlevm> qcTlevmList = qcTlevmMapper.selectALlTlevm(qcTlevm);
        return qcTlevmList;
    }
}
